package com.technoorbit.vbreakup;


import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;

public class NetworkConnection{
	//private XMPPClient xmppClient;
	public static XMPPConnection connection;
	public Handler mHandler = new Handler();
	
	public void EstablishConnection(String host,String  port,String  service,String UserName,String Password)
	{
		String[] setting= {host,port,service,UserName,Password};
		
        setConnection(setting);
        //new EstablishConnectionTask().execute(host, port, service,UserName,Password);
	}
	
	public void ConnectOnly(Context ct)
	{
		
		ConnectionConfiguration connConfig =
	            new ConnectionConfiguration(ct.getString(R.string.Host), Integer.parseInt(ct.getString(R.string.ClientPort)), ct.getString(R.string.Service));
	    connection = new XMPPConnection(connConfig);
	    connection=new XMPPConnection(connConfig);
	    try {
	    	connection.connect();
	        Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
	    } catch (XMPPException ex) {
	        Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
	        Log.e("XMPPClient", ex.toString());
	        connection=null;
	        
	    }
	}
	
 public void LoginOnly(String UserName,String Password){
	 
	 try {
	    	
		 connection.login(UserName,Password);
	        Log.i("XMPPClient", "Logged in as " + connection.getUser());

	        // Set the status to available
	        Presence presence = new Presence(Presence.Type.available);
	        connection.sendPacket(presence);
	     
	    } catch (XMPPException ex) {
	        Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + UserName);
	        Log.e("XMPPClient", ex.toString());
	        connection=null;
	        
	    }
 }
	
	
public  void setConnection(String[] setting){
	
	// Create a connection
    ConnectionConfiguration connConfig =
            new ConnectionConfiguration(setting[0], Integer.parseInt(setting[1]), setting[2]);
    connection = new XMPPConnection(connConfig);
    connection=new XMPPConnection(connConfig);
    try {
    	connection.connect();
        Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
    } catch (XMPPException ex) {
        Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
        Log.e("XMPPClient", ex.toString());
        connection=null;
        
    }
    try {
    	
    	connection.login(setting[3],setting[4]);
        Log.i("XMPPClient", "Logged in as " + connection.getUser());

        // Set the status to available
        Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);
     
    } catch (XMPPException ex) {
        Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + setting[3]);
        Log.e("XMPPClient", ex.toString());
        connection=null;
        
    }
  }	 
}

final class EstablishConnectionTask extends AsyncTask<String, Integer, Long> {
    // Do the long-running work in here
	XMPPConnection connection;
    protected Long doInBackground(String... setting) {

		// Create a connection
        ConnectionConfiguration connConfig =
                new ConnectionConfiguration(setting[0], Integer.parseInt(setting[1]), setting[2]);
        connection = new XMPPConnection(connConfig);
        connection=new XMPPConnection(connConfig);
        try {
        connection.connect();
            Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
            Log.e("XMPPClient", ex.toString());
            connection=null;            
        }
        try {
        	
        	connection.login(setting[3],setting[4]);
            Log.i("XMPPClient", "Logged in as " + connection.getUser());

            // Set the status to available
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
         
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + setting[3]);
            Log.e("XMPPClient", ex.toString());
            connection=null;
            
        }
      return (long) 12.2;
    }

    // This is called each time you call publishProgress()
    protected void onProgressUpdate(Integer... progress) {
       
    }

    // This is called when doInBackground() is finished
    protected void onPostExecute(Long result) {
        
    }
}
